﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using G15_Agent.Models.Travel;
using System.Web.Caching;
using G15_Agent.Helpers.ServiceHelpers;

namespace G15_Agent.Helpers.ServiceHelpers.TravelServiceHelpers
{
    public class TravelSerivce : Service
    {
        virtual protected List<TourDuLich> IRetrieveTourList(
            string tourType, 
            string departLocation)
        {
            return null;
        }

        virtual protected List<TourDuLich> ISearchForTour(
            string tourType, 
            DateTime departDate,
            DateTime returnDate, 
            double minPrice, 
            double maxPrice, 
            string route)
        {
            return null;
        }

        virtual protected TourDuLichChiTiet IRetrieveTourDetail(
            string tourID)
        {
            return null;
        }

        virtual protected int? IBookingForTour(
            int tourID, 
            int numberOfAdult, 
            int numberOfChilren, 
            List<KhachHang> listPassenger)
        {
            return null;
        }

        // Các phương thức để gọi ra bên ngoài

        public List<TourDuLich> RetrieveTourList(
            string tourType,
            string departLocation)
        {
            string key = 
                "serviceID" + this.ServiceID + 
                "tourType" + tourType + 
                "departLocation" + departLocation;
            List<TourDuLich> result = null;
            // Kiểm tra dữ liệu trong cache
            result = (List<TourDuLich>)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.IRetrieveTourList(tourType, departLocation);
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.IRetrieveTourList(tourType, departLocation);
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }                
            }
            
            return result;
        }

        public List<TourDuLich> SearchForTour(
            string tourType,
            DateTime departDate,
            DateTime returnDate,
            double minPrice,
            double maxPrice,
            string route)
        {
            string key =
                "serviceID" + this.ServiceID + 
                "tourType" + tourType + 
                "departDate" + departDate + 
                "returnDate" + returnDate + 
                "minPrice" + minPrice + 
                "maxPrice" + maxPrice + 
                "route" + route;

            List<TourDuLich> result = null;
            // Kiểm tra dữ liệu trong cache
            result = (List<TourDuLich>)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.ISearchForTour(tourType, departDate, returnDate, minPrice, maxPrice, route);
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = this.ISearchForTour(tourType, departDate, returnDate, minPrice, maxPrice, route);
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }

        public TourDuLichChiTiet RetrieveTourDetail(
            string tourID)
        {
            string key =
                "serviceID" + ServiceID +
                "tourID" + tourID;

            TourDuLichChiTiet result = null;
            result = (TourDuLichChiTiet)HttpRuntime.Cache[key];
            if (result == null)
            {
                try
                {
                    if (!isLogin)
                        this.Login();
                    result = this.IRetrieveTourDetail(tourID);
                }
                catch (Exception ex)
                {
                    if (this.Login())
                        result = result = this.IRetrieveTourDetail(tourID);
                }
                finally
                {
                    if (result != null)
                        HttpRuntime.Cache[key] = result;
                }
            }

            return result;
        }

        public int? BookingForTour(
            int tourID,
            int numberOfAdult,
            int numberOfChilren,
            List<KhachHang> listPassenger)
        {
            int? result = null;
            try
            {
                if (!isLogin)
                    this.Login();
                result = this.IBookingForTour(tourID, numberOfAdult, numberOfChilren, listPassenger);
            }
            catch (Exception ex)
            {
                if (this.Login())
                    result = this.IBookingForTour(tourID, numberOfAdult, numberOfChilren, listPassenger);
            }            

            return result;
        }
    }
}